package Doan;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

class SQLServer implements Runnable {
	private Socket sock;
	private BufferedInputStream bis;
	private DataInputStream in;
	private PrintStream out;
	String line1, line2, line3, line4;

	public SQLServer(Socket s) {
		sock = s;
	}

	public void run() {
		try {
			bis = new BufferedInputStream(sock.getInputStream());
			in = new DataInputStream(bis);
			out = new PrintStream(sock.getOutputStream());
			while (true) {
				line1 = in.readLine();// nhan du lieu tu client
				// out.flush();
				line2 = in.readLine();
				// out.flush();
				line3 = in.readLine();
				// out.flush();
				line4 = in.readLine();
				if (line1.startsWith("DangNhap")) {
					Dangnhap(line2, line3);

				}

				else if (line1.startsWith("XemSoDu"))
					Xemsodu(line2);
				else if (line1.startsWith("NapTien")) {
					int sotien = Integer.parseInt(line3);
					Nap(line2, sotien);
				} else if (line1.startsWith("RutTien")) {
					int sotien = Integer.parseInt(line3);
					Rut(line2, sotien);
				} else if (line1.startsWith("ChuyenKhoan")) {
					int sotien = Integer.parseInt(line4);
					Chuyenkhoan(line2, line3, sotien);
				} else if (line1.startsWith("DoiMatKhau"))
					Doimatkhau(line2, line3);
				else if (line1.startsWith("KiemTra"))
					Kiemtra(line2);
			}

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public String Getaccount(String account) {
		return account;

	}

	// Dang nhap tu client
	public void Dangnhap(String stk, String mk) throws Exception {
		int sodong = 0;
		String hoten = null;
		String url = "com.mysql.jdbc.Driver";
		String sql = "select* from khachhang where sotaikhoan='" + stk
				+ "' and matkhau='" + mk + "'";
		try {
			Class.forName(url);
			Connection cn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/atm", "root", "");
			PreparedStatement stmt = cn.prepareStatement(sql);
			ResultSet r = stmt.executeQuery();// thuc thi
			while (r.next()) {
				String sotk = r.getString("sotaikhoan");
				String makhau = r.getString("matkhau");
				hoten = r.getString("hoten");
				int sotien = r.getInt("tongtien");
				String dc = r.getString("diachi");
				String sodt = r.getString("sodienthoai");
				System.out.println(stk + "\t" + mk + "\t" + hoten + "\t"
						+ sotien + "\t" + dc + "\t" + sodt);
				sodong++;
			}
			cn.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		out.println(sodong);
		out.flush();
		out.println(hoten);
		out.flush();
	}

	// Xem so du tu client
  